//
//  HomeWhirligigWebViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/12/30.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDWebViewController.h"
#import <WebKit/WebKit.h>
//view
#import "FDNowDataView.h"

@interface FDWebViewController ()<WKNavigationDelegate>
/**webView*/
@property (nonatomic, strong) WKWebView *webView;
/**hud*/
@property (nonatomic, strong) MBProgressHUD *hud;

@property (nonatomic, strong) FDNowDataView *nodataView;

@end

@implementation FDWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"详情";
    
    [self setupUI];
    

    [self downloadWhirligigContentHTMLStrWithResource:self.resource];
}


- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    self.webView.navigationDelegate = self;
    
    [self.view addSubview:self.nodataView];
    self.nodataView.hidden = YES;//默认隐藏
}


//getter
- (FDNowDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[FDNowDataView alloc] initWithFrame:self.view.bounds];
    }
    return _nodataView;
}

//public/ca4bd7ba43254a87bd51e9f3436c9737
//!!!!:下载内容字符串
- (void)downloadWhirligigContentHTMLStrWithResource:(NSString *)resourece {
 
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    
//    resourece = @"public/ca4bd7ba43254a87bd51e9f3436c9737";
    
    //1.下载内容
    [FDAWSDownUpLoadTool fdAWSDownloadWithKey:resourece complectionBlock:^(NSString * _Nonnull resultStr) {
        [self.hud hideAnimated:YES];
        //2.替换内容 -->html字符串FDWebSocketTool
        NSString *htmlStr = [FDWebViewHtmlTool fdwebViewReplaceTemplateHtmlMainDivContentWithHtmlStr:resultStr];
        //3.记载HTML字符串
        [self.webView loadHTMLString:htmlStr baseURL:nil];
        
        //暂无数据
        self.nodataView.hidden = YES;
        
    } failBlock:^{
        
        [self.hud hideAnimated:YES];
        
        //暂无数据
        self.nodataView.hidden = NO;
    }];
}


//!!!! -getter
- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *configure = [[WKWebViewConfiguration alloc] init];
        
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:(configure)];
    }
    return _webView;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    FDLog(@"加载成功:");
    
    [self.hud hideAnimated:YES];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    FDLog(@"加载失败:%@",error);
    
    [self.hud hideAnimated:YES];
}

@end

